﻿using FAQ_UI.window;
using java.lang;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using yzrilyzr.FAQ.client;
using yzrilyzr.FAQ.constants;
using yzrilyzr.FAQ.protocol;
using yzrilyzr.util;
using Math = System.Math;

namespace FAQ_UI
{
	/// <summary>
	/// ToolBar.xaml 的交互逻辑
	/// </summary>
	public partial class ToolBar : Page, MsgPackReceiver, INotifyPropertyChanged
	{
		private Logger logger = new Logger("ToolBar");
		private double targetPos = 0;
		private double selectorPos = 0;
		private TextBlock last, current;
		public static readonly int TOOLBAR_SELECTOR = 30000;
		public string StateText { get; set; }

		public ToolBar()
		{
			InitializeComponent();
			DataContext = this;
			logger.i("Init");
			current = T0;
			current.Foreground = Theme.AccentBrush;
			FAQUtil.client.addReceiver(this);
			DragView.Background = Theme.BackBrush;
			StateText = FAQClient.getOnlineState(FAQUtil.client.getFAQID()) ;
			FAQUtil.client.ping(5000);
		}

		public event PropertyChangedEventHandler PropertyChanged;

		protected void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}

		public Brush AccentColor
		{ get { return Theme.AccentBrush; } }

		public Drawable Avatar
		{ get { return FAQUtil.getAvatar(FAQUtil.client.getFAQID()); } }

		private void selectTab(object sender, MouseButtonEventArgs e)
		{
			last = current;
			current = (TextBlock)sender;
			last.Foreground = Theme.TextMainBrush;
			current.Foreground = Theme.AccentBrush;
			string name = ((FrameworkElement)sender).Name;
			logger.i(name);
			int index = Integer.parseInt(name.Substring(1));
			targetPos = index * 40;
			btnCountAsync_Click();
			FAQUtil.eventBus.asyncSend(new BinMsgPack(TOOLBAR_SELECTOR).put(K.DATA, index));
		}

		private void exit(object sender, MouseButtonEventArgs e)
		{
			FAQUtil.eventBus.asyncSend(new BinMsgPack(App.APPLICATION_CLOSE));
		}

		private void settings(object sender, MouseButtonEventArgs e)
		{
			FAQUtil.eventBus.asyncSend(new BinMsgPack(App.OPEN_SETTINGS));
		}

		private async void btnCountAsync_Click()
		{
			while (Math.Abs(targetPos - selectorPos) > 0.5)
			{
				selectorPos += (targetPos - selectorPos) * 0.2;
				await UpdateUI();
			}
		}

		private void myProfile(object sender, MouseButtonEventArgs e)
		{
			new UserProfile(FAQUtil.client.getFAQID()).Show();
		}

		private async Task UpdateUI()
		{
			tabSelector.Margin = new Thickness(0, 5 + selectorPos, 0, 5);
			await Task.Delay(6);
		}

		private void dragWindow(object sender, MouseButtonEventArgs e)
		{
			Window.GetWindow(this).DragMove();
		}

		public void receive(MsgPackTransfer mpt, MsgPack p)
		{
			if (p.getCmd() == C.USER_CACHE)
			{
				Dispatcher.Invoke(new Action(() =>
				{
					OnPropertyChanged(nameof(Avatar));
				}));
			}else if (p.getCmd() == C.PING_PONG)
			{
				Dispatcher.Invoke(new Action(() =>
				{
					bool ol = p.getBoolean(K.DATA);
					StateText = ol ? FAQClient.getOnlineState(FAQUtil.client.getFAQID()) : "离线";
					OnPropertyChanged(nameof(StateText));
				}));
			}
		}
	}
}